home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / midi / gfft.lha / gfft-2.03 / source / gfft-2.03-source.lha / wbprogrs.c < prev    next >
C/C++ Source or Header  |  1996-01-02  |  5KB  |  192 lines

  1. /***************************************************************************
  2.  *          Copyright (C) 1995  Charles P. Peterson                  *
  3.  *         4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
  4.  *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
  5.  *                                                                         *
  6.  *          This is free software with NO WARRANTY.                  *
  7.  *          See gfft.c, or run program itself, for details.              *
  8.  *              Support is available for a fee.                      *
  9.  ***************************************************************************
  10.  *
  11.  * Program:     gfft--General FFT analysis
  12.  * File:        wbprogrs.c
  13.  * Purpose:     progress requester 
  14.  * Author:      Charles Peterson (CPP)
  15.  * History:     4-Feb-95 (1.31) CPP; Created.
  16.  *              10-Feb-95 (1.36) CPP; Allow interrupt from CLI
  17.  * Comments:    Workbench GUI.  Amiga Dependent!
  18.  *              This may be called from different places for different
  19.  *                progress requesters.
  20.  *              HOWEVER, IT IS NOT RE-ENTRANT.  THERE MUST ONLY BE ONE
  21.  *                ACTIVE PROGRESS REQUESTER AT ANY ONE TIME (in any one
  22.  *                task, anyway).
  23.  */
  24.  
  25. #ifdef AMIGA  /* This module is AMIGA dependent */
  26.  
  27. /* #define CHECK_PROGRESS_RATE */
  28.  
  29. #include <stdio.h>     /* sprintf() */
  30. #include <string.h>    /* strlen() */
  31.  
  32. /*
  33.  * Amiga includes
  34.  */
  35. #include <exec/types.h>
  36. #include <workbench/workbench.h>
  37. #include <intuition/intuition.h>
  38. #include <clib/intuition_protos.h>
  39. #include <clib/exec_protos.h>
  40. #include <dos.h> /* chkabort() */
  41.  
  42. /*
  43.  * GFFT includes
  44.  */
  45. #include "gfft.h"
  46. #include "wbench.h"
  47.  
  48. static struct Window *base_window_p = NULL;
  49. static struct Gadget *static_stop_progress_gadgetp = NULL;
  50. static struct Gadget *static_progress_gadgetp = NULL;
  51.  
  52. #define STRLEN_STOP 4
  53.  
  54.  
  55. struct Requester *progress_requester__new (char *message_text,
  56.                        int min_progress_width)
  57. {
  58.     struct IntuiText *line1_textp;
  59.     struct Gadget *stop_progress_gadgetp = NULL;
  60.     struct Requester *progress_rp = NULL;
  61.     int text_width = strlen (message_text);
  62.     int progress_width;
  63.     int text_indent = 0;
  64.     int stop_indent = 0;
  65.  
  66.     progress_width = (min_progress_width > text_width) ?
  67.       min_progress_width : text_width;
  68.  
  69.     if (progress_width < STRLEN_STOP) progress_width = STRLEN_STOP;
  70.  
  71.     if (progress_width > text_width) text_indent =
  72.       (progress_width - text_width) / 2;
  73.  
  74.     if (progress_width > STRLEN_STOP) stop_indent =
  75.       (progress_width - STRLEN_STOP) / 2;
  76.  
  77.     gadget__begin (GTYP_REQGADGET);
  78.  
  79.     line1_textp = requester_text__new (message_text, text_indent, 0);
  80.  
  81.     progress_gadget__new (progress_width, 0, 1);
  82.  
  83.     stop_progress_gadgetp = tall_action_button__new ("STOP", 
  84.                               stop_indent, 3);
  85.  
  86.     progress_rp = requester__new ();
  87.     progress_rp->ReqGadget = stop_progress_gadgetp;
  88.     progress_rp->ReqText = line1_textp;
  89.  
  90.     return progress_rp;
  91. }
  92.  
  93.  
  94. char *progress_requester__apply (struct Requester *progress_rp,
  95.                  char *command_function(char *command),
  96.                  char *command,
  97.                  struct Window *windowp)
  98. {
  99.     struct IntuiMessage *message;
  100.     char *return_arguments = NULL;
  101.     BOOLEAN error_caught = FALSE;
  102.  
  103.     if (requester__display (progress_rp, windowp))
  104.     {
  105.     base_window_p = windowp;
  106.     static_stop_progress_gadgetp = progress_rp->ReqGadget;
  107.     static_progress_gadgetp = progress_rp->ReqGadget->NextGadget;
  108.     progress_gadget__clear (static_progress_gadgetp, windowp);
  109. /*
  110.  * Now, execute command, catching errors, if any
  111.  * (stop is raised like an error)
  112.  */
  113.     CATCH_ERROR
  114.     {
  115.         return_arguments = (*command_function)(command);
  116.     }
  117.     ON_ERROR
  118.     {
  119.         return_arguments = NULL;
  120.         error_caught = TRUE;
  121.     }
  122.     END_CATCH_ERROR;
  123.  
  124.     while (message = (struct IntuiMessage *) GetMsg 
  125.            (base_window_p->UserPort))
  126.     {
  127.         ReplyMsg ((struct Message *) message);
  128.     }
  129.     requester__remove (progress_rp, base_window_p);
  130.  
  131.     base_window_p = NULL;
  132.  
  133.     if (error_caught)
  134.     {
  135.         RAISE_ERROR (NOTHING_SPECIAL);
  136.     }
  137.     }
  138.     else  /* Couldn't display requester.  Probably memory error. */
  139.     {
  140.     error_message (OUT_OF_MEMORY);
  141.     RAISE_ERROR (NOTHING_SPECIAL);
  142.     }
  143.     return return_arguments;
  144. }
  145.  
  146. void progress_requester__update (int percent_done)
  147. {
  148.     progress_requester__check_stop ();
  149.  
  150.     if (base_window_p)
  151.     {
  152.     progress_gadget__update (static_progress_gadgetp, percent_done, 
  153.                  base_window_p);
  154.     }
  155.     return;
  156. }
  157.  
  158. void progress_requester__check_stop (void)
  159. {
  160.     struct IntuiMessage *message;
  161.     APTR *address = NULL;
  162.  
  163. #ifdef CHECK_PROGRESS_RATE
  164.     static int i = 0;
  165.     printf ("Stop test: %d\n", ++i);
  166. #endif
  167.  
  168.     if (!base_window_p)   /* progress requester not active */
  169.     {
  170.     chkabort();
  171.     if (Interrupt_Count)
  172.     {
  173.         printf ("\n***BREAK\n");
  174.         RAISE_ERROR (NOTHING_SPECIAL);
  175.     }
  176.     return;
  177.     }
  178.  
  179.     while (message = (struct IntuiMessage *) GetMsg 
  180.        (base_window_p->UserPort))
  181.     {
  182.     address = message->IAddress;
  183.     ReplyMsg ((struct Message *) message);
  184.     if (address == (APTR) static_stop_progress_gadgetp)
  185.     {
  186.         RAISE_ERROR (NOTHING_SPECIAL);
  187.     }
  188.     }
  189. }
  190.  
  191. #endif /* end ifdef AMIGA */
  192.